package com.hx.mypetstore.ui.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.support.hwid.HuaweiIdAuthManager;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParams;
import com.huawei.hms.support.hwid.request.HuaweiIdAuthParamsHelper;
import com.huawei.hms.support.hwid.service.HuaweiIdAuthService;
import com.hx.mypetstore.R;
import com.hx.mypetstore.common.ICallBack;
import com.hx.mypetstore.constant.SPConstants;
import com.hx.mypetstore.ui.push.PushConst;
import com.hx.mypetstore.ui.push.PushService;
import com.hx.mypetstore.ui.push.PushSharedPreferences;
import com.hx.mypetstore.util.LoginUtil;
import com.hx.mypetstore.util.SPUtil;
import com.hx.mypetstore.util.ToastUtil;
import com.hx.mypetstore.util.view.SignOutDialog;

import java.util.Map;

public class SettingActivity extends AppCompatActivity{

    private ImageView imVback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        // 初始化View
        initView();

    }

    private void initView() {
        imVback = findViewById(R.id.title_back);
        imVback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回
                finish();
            }
        });
//        findViewById(R.id.setting_address).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 地址管理
//                startActivity(new Intent(SettingAct.this, AddAddressManageAct.class));
//            }
//        });
        findViewById(R.id.setting_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 退出登录
                onLoginCheck();
            }
        });
    }

    private void onLoginCheck() {
        boolean isHuaweiLogin = LoginUtil.isHuaweiLogin(this);
        if (!isHuaweiLogin) {
            onExitLogin();
            return;
        }

        // 显示是否退出华为账号
        FragmentManager manager = getSupportFragmentManager();
        DialogFragment fragment = (DialogFragment) manager.findFragmentByTag("mSignOutDialog");
        if ((fragment != null) && (fragment.getDialog() != null) && (fragment.getDialog().isShowing())) {
            return;
        }
        SignOutDialog mSignOutDialog = SignOutDialog.newInstance();
        mSignOutDialog.setCallBack(new ICallBack<Integer>() {
            @Override
            public void onSuccess(Integer bean) {
                if (bean == 1) {
                    // 退出华为账号
                    huaweiSignOut();
                } else {
                    // 不退出华为账号
                    onExitLogin();
                }
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
        mSignOutDialog.show(getSupportFragmentManager(), "mSignOutDialog");
    }

    private void huaweiSignOut() {
        HuaweiIdAuthParams authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();
        HuaweiIdAuthService authService = HuaweiIdAuthManager.getService(SettingActivity.this, authParams);
        Task<Void> signOutTask = authService.signOut();
        signOutTask.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                //Log.d(TAG, "signOut Success");
                // TODO 退出到首页， 不删除华为帐号数据
                SPUtil.put(SettingActivity.this, SPConstants.KEY_HW_LOGIN, false);
                onExitLogin();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
               //Log.d(TAG, "signOut fail");
                ToastUtil.getInstance().showShort(SettingActivity.this, "signOut fail");
            }
        });
    }

    private void onExitLogin() {
        SPUtil.put(this, SPConstants.KEY_LOGIN, false);
        pushClear();
        finish();
    }

    /**
     * push clear
     */
    private void pushClear() {
        Context context = getApplicationContext();
        Map<String, String> topics = PushSharedPreferences.readTopic(context);
        for (String topic : topics.keySet()) {
            PushService.unsubscribe(context, topic);
        }
        PushSharedPreferences.clearTopic(context);
        PushSharedPreferences.clearMessage(context);
        PushSharedPreferences.saveConfig(context, PushConst.PUSH_MESSAGE_SWITCH, String.valueOf(true));
        PushService.turnOnOff(context, true);
    }
}